package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.content.convert.CourseMarketConvert;
import com.xuecheng.content.entity.CourseMarket;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.service.CourseMarketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程营销信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseMarketServiceImpl extends ServiceImpl<CourseMarketMapper, CourseMarket> implements CourseMarketService {


    /*
      事务分析
     条件：  dto-->po / CourseBaseDTO --> courseMarket
     将 po 添加到表中 根据返回值做判断
     不需要返回值
     */
    public CourseMarket createCourseMarket(CourseBaseDTO courseBaseDTO) {
        CourseMarket courseMarket = CourseMarketConvert.INSTANCE.dto2entity(courseBaseDTO);
        courseMarket.setPrice(courseBaseDTO.getPrice().floatValue());

        boolean saveMarket = this.save(courseMarket);
        if (!saveMarket){
            throw new RuntimeException("添加课程营销信息失败");
        }

        return courseMarket;
    }


    /*
    根据课程ID查询信息
     */
    public CourseMarket queryCourseMarket(Long courseBaseId) {

        LambdaQueryWrapper<CourseMarket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseMarket::getCourseId,courseBaseId);
        CourseMarket market = this.getOne(queryWrapper);
        return market;
    }

    @Override
    public CourseMarket modifyCourseMarket(CourseBaseDTO courseBaseDTO) {
        CourseMarket courseMarket = CourseMarketConvert.INSTANCE.dto2entity(courseBaseDTO);
        LambdaQueryWrapper<CourseMarket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseMarket::getCourseId,courseBaseDTO.getCourseBaseId());
        boolean updateMarket = this.update(courseMarket, queryWrapper);
        if (!updateMarket){
            throw new RuntimeException("课程营销信息修改失败");
        }
        return courseMarket;
    }
}
